package com.tuiyun.jxgl.action;

import java.util.Date;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.dao.DriverHome;
import com.tuiyun.jxgl.dao.StudentHome;
import com.tuiyun.util.PWDUtil;

public class RegAction {

	private StudentHome sh;
	private DriverHome dh;
	
	private String username;
	private String realName;
	private String password;
	private int age = 0;
	private byte gender;
	private String phone;
	private String address;
	private byte carType;
	private String idcard;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public byte getGender() {
		return gender;
	}

	public void setGender(byte gender) {
		this.gender = gender;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public StudentHome getSh() {
		return sh;
	}

	public void setSh(StudentHome sh) {
		this.sh = sh;
	}
	
	private String error_msg;
	
	private String jsonstring;
	@Transactional
	public String checkName(){
		if(sh.findByUsername(getUsername()) != null || dh.findByUsername(getUsername()) != null){
			jsonstring = "false";
		}else{
			jsonstring = "true";
		}
		return "json";
	}
	@Transactional
	public String execute(){
		if(sh.findByUsername(getUsername()) != null || dh.findByUsername(getUsername()) != null){
			error_msg = "用户名已经存在！";
			return "error";
		}
		Student s = new Student();
		s.setAddress(getAddress());
		s.setAge(getAge());
		s.setRealName(getRealName());
		s.setGender(getGender());
		s.setName(getUsername());
		s.setPhone(getPhone());
		s.setIdcard(getIdcard());
		s.setCarType(getCarType());
		//ByteSource bs = ByteSource.Util.bytes("jiaxiaotuiyunguanli");
		//String salt = bs.toHex();
		//String hashedPasswordBase64 = new Sha256Hash(getPassword(), salt, 1024).toBase64();
		s.setPwd(PWDUtil.encryptPWD(getPassword()));
		s.setRegdate(new Date(System.currentTimeMillis()));
		s.setState(Student.REGED_BUT_NO_TA);
		sh.persist(s);
		
		//add auto login
		
		UsernamePasswordToken token = new UsernamePasswordToken(getUsername(),
				PWDUtil.encryptPWD(getPassword()));		
		try {
			Subject currentUser = SecurityUtils.getSubject();		
			currentUser.login(token);
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "success";
	}

	public String getError_msg() {
		return error_msg;
	}

	public void setError_msg(String error_msg) {
		this.error_msg = error_msg;
	}

	public String getJsonstring() {
		return jsonstring;
	}

	public void setJsonstring(String jsonstring) {
		this.jsonstring = jsonstring;
	}

	public byte getCarType() {
		return carType;
	}

	public void setCarType(byte carType) {
		this.carType = carType;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public DriverHome getDh() {
		return dh;
	}

	public void setDh(DriverHome dh) {
		this.dh = dh;
	}
	
}
